<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <title>安全保护区在线审查系统</title>
    <meta name="renderer" content="webkit" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
    <meta
      name="viewport"
      content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0"
    />
    <link
      rel="stylesheet"
      href="../../layuiadmin/layui/css/layui.css"
      media="all"
    />
    <link
      rel="stylesheet"
      href="../../layuiadmin/style/admin.css"
      media="all"
    />
    <link
      rel="stylesheet"
      href="../../layuiadmin/style/style.css"
      media="all"
    />
    <link
      rel="stylesheet"
      href="../../layuiadmin/style/login.css"
      media="all"
    />
  </head>
  <body class="layui-layout-body" onkeydown="keyLogin();">
    <div class="login">
      <div class="loginBgTop"></div>
      <h1 class="loginTitle">地铁保护区在线审查系统</h1>
      <div class="loginBox">
        <h3>用户登录</h3>
        <div
          class="layadmin-user-login-box layadmin-user-login-body layui-form"
        >
          <div class="layui-form-item">
            <label
              class="layadmin-user-login-icon layui-icon userNameIcon"
              for="LAY-user-login-username"
            ></label>
            <input
              type="text"
              name="username"
              id="LAY-user-login-username"
              lay-verify="required"
              placeholder="请输入用户名"
              class="layui-input"
            />
          </div>
          <div class="layui-form-item">
            <label
              class="layadmin-user-login-icon layui-icon passwordIcon"
              for="LAY-user-login-password"
            ></label>
            <input
              type="password"
              name="password"
              id="LAY-user-login-password"
              lay-verify="required"
              placeholder="请输入密码"
              class="layui-input"
            />
          </div>
          <div class="layui-form-item">
            <button
              id="loginButton"
              class="layui-btn layui-btn-fluid"
              lay-submit
              lay-filter="LAY-user-login-submit"
              style="background-color: #409eff"
            >
              登录
            </button>
          </div>
        </div>
      </div>
      <footer class="footer">上海地铁监护管理有限公司版权所有</footer>
    </div>
    <script src="../../layuiadmin/layui/layui.js"></script>
    <script src="../../layuiadmin/modules/const_config.js"></script>
    <script>
      layui
        .config({
          base: "../../layuiadmin/", //静态资源所在路径
        })
        .extend({
          index: "lib/index", //主入口模块
        })
        .use(["index", "form"], function () {
          var $ = layui.$,
            setter = layui.setter,
            form = layui.form;
          // 获取URL 的PARAM参数
          function getQueryString(name) {
            var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
            var r = window.location.search.substr(1).match(reg);
            if (r != null) {
              return unescape(r[2]);
            }
            return null;
          }
          if (getQueryString("jwt")) {
            layui.sessionData("userToken", {
              key: "key",
              value: "Bearer  " + getQueryString("jwt"),
            });
            $.ajax({
              url: base_url + "/user/info",
              headers: {
                Accept: "application/json; charset=utf-8",
                Authorization: layui.sessionData("userToken").key,
              },
              async: true,
              type: "get",
              success: function (req) {
                if (req.code == "200") {
                  layui.sessionData("userName", {
                    key: "key",
                    value: req.data.userName,
                  });
                  layui.sessionData("user", {
                    key: "key",
                    value: req.data.userNumber,
                  });
                  location.href = "../index.html";
                }
              },
            });

            location.href = "../index.html"; //后台主页
          } else {
          }
          form.on("submit(LAY-user-login-submit)", function (data) {
            var data = data.field;
            //请求登入接口

            $.post({
              url: base_url + "/api/login",
              data: data,
              success: function (req, textStatus, request) {
                if (req.code == "200") {
                  let tokenid = "";
                  tokenid = request.getResponseHeader("token");
                  layui.sessionData("userToken", {
                    key: "key",
                    value: tokenid,
                  });
                  layui.sessionData("userName", {
                    key: "key",
                    value: data.username,
                  });
                  layui.sessionData("user", {
                    key: "key",
                    value: req.data.username,
                  });
                  location.href = "../index.html"; //后台主页
                } else {
                  layer.msg("登录失败，" + req.message, {
                    offset: "15px",
                    icon: 1,
                    time: 3000,
                  });
                }
              },
            });
          });
        });
      function keyLogin() {
        if (event.keyCode == 13)
          //回车键的键值为13
          document.getElementById("loginButton").click(); //调用登录按钮的登录事件
      }
    </script>
  </body>
</html>
